//
//  XXTabBarController.swift
//  OfferManager
//
//  Created by IT.Bear on 2018/1/4.
//  Copyright © 2018年 OFFER. All rights reserved.
//

import UIKit

class XXTabBarController: UITabBarController {
    
    let SCREEN_WIDTH = UIScreen.main.bounds.width
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.tabBar.isTranslucent = false
        
        setUpTabBar()
        
        setChildVC()
        
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    fileprivate func setUpTabBar (){
        //根据颜色值画条线
        let rect = CGRect(x:0,y:0,width:SCREEN_WIDTH,height:0.5)
        UIGraphicsBeginImageContext(rect.size)
        let context = UIGraphicsGetCurrentContext()!
        context.setFillColor(UIColor.rgbColorFromHex(rgb: 0xeeeeee).cgColor)
        context.fill(rect)
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        
        
        //这两个很主要缺一不可
        self.tabBar.shadowImage = image
        self.tabBar.backgroundImage = UIImage()
        
        // 设置字体偏移
        UITabBarItem.appearance().titlePositionAdjustment = UIOffsetMake(0.0, -5.0)
        
        //tabBar 底部工具栏背景颜色 (以下两个都行)
        //self.tabBar.barTintColor = UIColor.orange
        self.tabBar.backgroundColor = UIColor.white
        
        //设置 tabBar 工具栏字体颜色 (未选中  和  选中)
        //未选中
        UITabBarItem.appearance().setTitleTextAttributes(NSDictionary(object:UIColor.rgbaColorFromHex(rgb: 0x999999, alpha: 1),forKey:NSAttributedStringKey.foregroundColor as NSCopying) as? [NSAttributedStringKey : Any], for: .normal)
        UITabBarItem.appearance().setTitleTextAttributes(NSDictionary(object:UIColor.black,forKey:NSAttributedStringKey.foregroundColor as NSCopying) as? [NSAttributedStringKey : Any], for: .selected)
        
        
        // 设置图片选中时颜色必须设置（系统默认选中蓝色）
        UITabBar.appearance().tintColor = UIColor.black
        //或者写法都是一样的
        //self.tabBar.tintColor = UIColor.black
    }
    
    //MARK:添加控制器
    fileprivate func setChildVC(){
        setupOneChildViewController(OFProduct_VC(), title: "商品", normalImage: "product_normal", selectedImage: "product_selected")
        
        setupOneChildViewController(OFPhoto_VC(), title: "图片", normalImage: "photo_normal", selectedImage: "photo_selected")
        
        
        setupOneChildViewController(OFMessage_VC(), title: "消息", normalImage: "message_normal", selectedImage: "message_selected")
        
        setupOneChildViewController(OFSetting_VC(), title: "我的", normalImage: "setting_normal", selectedImage: "setting_selected")
    }
    
    
    
    fileprivate func setupOneChildViewController(_ vc:UIViewController,title:String, normalImage:String, selectedImage:String){
        
        vc.tabBarItem.title = title
        vc.tabBarItem.image = UIImage(named:normalImage)
        vc.tabBarItem.selectedImage = UIImage(named:selectedImage)
        let baseNav = UINavigationController(rootViewController:vc)
        addChildViewController(baseNav)
    }
    
    
    /*
     // MARK: - Navigation
     
     // In a storyboard-based application, you will often want to do a little preparation before navigation
     override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
     // Get the new view controller using segue.destinationViewController.
     // Pass the selected object to the new view controller.
     }
     */
    
}
